package net.sf.rose.business.bean.control;

import java.util.ArrayList;
import java.util.List;

import net.sf.rose.business.bean.control.judge.JudgeBean;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;

/**
 * @author fengjian
 * @since 2014-5-20
 * 
 */
public class MenuBean {

	/** 菜单编号 */
	@Attribute
	private String id = "";

	/** 菜单名称 */
	@Attribute
	private String text = "";

	/** 菜单代码 */
	@Attribute(required = false)
	private String code = null;

	/** 子菜单编号序号 最大值是999，最小值为1 */
	@Attribute
	private int count = 0;

	/** 菜单操作名称 */
	@Element(required = false)
	private String actionName = null;

	/** 视图名称 */
	@Element(required = false)
	private String view = null;

	/** 操作类型(Window,Dialog,Message) */
	@Element(required = false)
	private String openType = null;
	public static String OPEN_WINDOW_TYPE = "window";
	public static String OPEN_DIALOG_TYPE = "dialog";
	public static String OPEN_MESSAGE_TYPE = "message";

	/** 权限配置 */
	@Element(required = false)
	private JudgeBean judge = null;

	/** 排序 */
	@Element(required = false)
	private int sort = 0;

	/** 子菜单组序列 */
	@ElementList(required = false)
	private List<MenuBean> childMenus = null;

	/** 说明 */
	@Element(required = false)
	private String description = null;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getSort() {
		return sort;
	}

	public void setSort(int sort) {
		this.sort = sort;
	}

	public List<MenuBean> getChildMenus() {
		if (childMenus == null) {
			childMenus = new ArrayList<MenuBean>();
		}
		return childMenus;
	}

	public void setChildMenus(List<MenuBean> childMenus) {
		this.childMenus = childMenus;
	}

	public void addChildMenu(MenuBean data) {
		if (childMenus == null) {
			childMenus = new ArrayList<MenuBean>();
		}
		count++;
		if (count < 10) {
			data.setId("00" + count);
		} else if (count < 100) {
			data.setId("0" + count);
		} else {
			data.setId(count + "");
		}
		data.setId(this.id + data.getId());
		data.setSort(childMenus.size());
		childMenus.add(data);
	}

	public boolean removeChildMenus(String menuID) {
		if (childMenus == null) {
			return false;
		}
		for (int i = 0; i < childMenus.size(); i++) {
			MenuBean child = childMenus.get(i);
			if (child.getId().equals(menuID)) {
				childMenus.remove(i);
				return true;
			}
		}
		return false;
	}

	public void update(MenuBean menu) {
		this.id = menu.id;
		this.code = menu.code;
		this.text = menu.text;
		this.actionName = menu.actionName;
		this.view = menu.view;
		this.openType = menu.openType;
		this.description = menu.description;
		this.judge = menu.judge;
		this.sort = menu.sort;
	}

	public String getView() {
		return view;
	}

	public void setView(String view) {
		this.view = view;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public JudgeBean getJudge() {
		return judge;
	}

	public void setJudge(JudgeBean judge) {
		this.judge = judge;
	}

	public String getOpenType() {
		return openType;
	}

	public void setOpenType(String openType) {
		this.openType = openType;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
